/*
* Copyright (c) 2016 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
*/
package com.hyyt.shopjsp.util.redis.util;

import com.hyyt.shopjsp.basic.pojo.BasicKeyBook;
import com.hyyt.shopjsp.basic.vo.BasicStatusRelateVo;
import com.hyyt.shopjsp.home.pojo.ShopHomeKeyBook;
import com.hyyt.shopjsp.phone.pojo.PhoneHomeKeyBook;
import com.hyyt.shopjsp.product.vo.AllProductTypeVo;
import com.hyyt.shopjsp.util.redis.service.impl.RedisBasicService;
import com.hyyt.shopjsp.util.common.SpringBeanUtil;
import com.hyyt.shopjsp.util.common.StringStaticKey;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.exception.ServiceException;
import com.hyyt.shopjsp.util.json.FastJsonUtils;

import java.util.*;

/**
 * Redis 常用读取工具类
 * Created by ankang on 2016/7/1 0001.
 */

public class RedisDataReadUtil {
    /**
     * 获取redis操作bean
     */
    private static RedisBasicService redisBasicService = SpringBeanUtil.getBean("redisBasicService");

    /**
     * 根据键名获取systemConfig对应的值
     *
     * @return
     */
    public static String getSysConfig(String key) {
        String redisValue = "";
        if (Utils.stringIsNotEmpty(key)) {
            redisValue = redisBasicService.getValue(StringStaticKey.SYSTEMCONFIG_PREFIX + key);
        }
        return redisValue;
    }

    /**
     * 根据键名获取BasicKeyBook对应的值
     *
     * @return
     */
    public static List<BasicKeyBook> getBasicKeyBook(String key) {
        if (!Utils.stringIsNotEmpty(key)) throw new ServiceException("参数传递有误！");
        String jsonString = redisBasicService.getValue(StringStaticKey.KEYBOOK_PREFIX + key);
        return FastJsonUtils.toList(jsonString, BasicKeyBook.class);
    }

    /**
     * 根据键名获取BasicKeyBook对应的值
     *
     * @return
     */
    public static List<ShopHomeKeyBook> getHomeKeyBook(String key) {
        if (!Utils.stringIsNotEmpty(key)) throw new ServiceException("参数传递有误！");
        String jsonString = redisBasicService.getValue(StringStaticKey.HOME_KEYBOOK_PREFIX + key);
        return FastJsonUtils.toList(jsonString, ShopHomeKeyBook.class);
    }

    /**
     * 根据键名获取PhoneHomeKeyBook对应的值
     *
     * @return
     */
    public static List<PhoneHomeKeyBook> getPhoneKeyBook(String key) {
        if (!Utils.stringIsNotEmpty(key)) throw new ServiceException("参数传递有误！");
        String jsonString = redisBasicService.getValue(StringStaticKey.PHONE_KEYBOOK_PREFIX + key);
        return FastJsonUtils.toList(jsonString, PhoneHomeKeyBook.class);
    }

    /**
     * 获取商品分类
     *
     * @return
     */
    public static List<AllProductTypeVo> getProductType() {
        String jsonString = redisBasicService.getValue(StringStaticKey.PRODUCT_TYPE);
        return FastJsonUtils.toList(jsonString, AllProductTypeVo.class);
    }


    /**
     * 根据状态类型获取对于业务状态bean集合
     *
     * @param statusType 状态类型，传于时需要在StringStaticKey中配置
     * @return
     */
    public static List<BasicStatusRelateVo> getStatusRelateListByType(String statusType) {
        if (!Utils.stringIsNotEmpty(statusType)) throw new ServiceException("参数传递有误！");
        String jsonString = redisBasicService.getValue(statusType);
        List<BasicStatusRelateVo> statusRelateVoList = FastJsonUtils.toList(jsonString, BasicStatusRelateVo.class);
        return statusRelateVoList;
    }

    /**
     * 根据状态类型和状态键名获取对于的状态值
     *
     * @param statusType 状态类型，传于时需要在StringStaticKey中配置
     * @param codeKey    状态键名
     * @return
     */
    public static String getStatusValue(String codeKey, String statusType) {
        if (!Utils.stringIsNotEmpty(codeKey) || !Utils.stringIsNotEmpty(statusType))
            throw new ServiceException("参数传递有误！");
        List<BasicStatusRelateVo> statusRelateVoList = getStatusRelateListByType(statusType);
        String result = null; //定义返回结果
        for (BasicStatusRelateVo statusRelate : statusRelateVoList) {
            if (codeKey.equals(statusRelate.getCodeKey())) {
                result = statusRelate.getValue();
                break;
            }
        }
        return result;
    }


    /**
     * 根据状态类型和状态值获取对于的状态皱键,排除
     *
     * @param statusType 状态类型，传于时需要在StringStaticKey中配置
     * @param value      状态值
     * @return
     */
    public static String getStatusKey(Integer value, String statusType) {
        if (value == null || !Utils.stringIsNotEmpty(statusType))
            throw new ServiceException("参数传递有误!");
        List<BasicStatusRelateVo> statusRelateVoList = getStatusRelateListByType(statusType);
        String result = null; //定义返回结果
        for (BasicStatusRelateVo statusRelate : statusRelateVoList) {
            if (value.equals(Integer.parseInt(statusRelate.getValue()))) {
                /**
                 * 排除订单中的付款类型
                 */
                if (StringStaticKey.ORDERS_STATUS.equals(statusType)) {
                    if (StringStaticKey.SETTLEMENT_STATUS.indexOf(statusRelate.getType()) != -1) {
                        continue;
                    } else {
                        result = statusRelate.getName();
                        break;
                    }
                } else {
                    result = statusRelate.getName();
                    break;
                }
            }

        }
        return result;
    }

    /**
     * 获取状态业务对象
     *
     * @param ordersState      订单状态值
     * @param settlementStatus 付款状态值
     * @param roleType         角色类型
     * @return
     */
    public static BasicStatusRelateVo getOrdersStatusRelate(String ordersState, Integer settlementStatus, Integer roleType) {
        if (ordersState == null || settlementStatus == null || roleType == null) throw new ServiceException("参数传递有误！");
        String statusType = StringStaticKey.ORDERS_STATUS;//订单状态类型
        BasicStatusRelateVo basicStatusRelateVo = null;//定义返回结果
        List<BasicStatusRelateVo> statusRelateVoList = getStatusRelateListByType(statusType);//获取结果
        if (Utils.collectionIsNotEmpty(statusRelateVoList)) {
            for (BasicStatusRelateVo statusRelateVo : statusRelateVoList) {//循环判断取值
                String voValue = statusRelateVo.getValue();      //状态值
                Integer voRoteType = statusRelateVo.getRoleType();//角色类型
                String operates = statusRelateVo.getOperates();     //可用操作
                String unionKey = statusRelateVo.getUnionKey();     //组合键
                if (ordersState.equals(voValue) && roleType.equals(voRoteType)) {
                    basicStatusRelateVo = statusRelateVo;//选取实体
                    if (Utils.stringIsNotEmpty(operates)) {//处理操作类型，不为空时转json
                        basicStatusRelateVo.setOperatesList(FastJsonUtils.toList(operates, Map.class));
                    }
                    basicStatusRelateVo.setSettlementStatusName(getStatusKey(settlementStatus, StringStaticKey.SETTLEMENT_STATUS));//获取付款类型文字说明
                    basicStatusRelateVo.setSettlementStatus(settlementStatus);//获取付款类型值，处理列表颜色显示问题
                    if (Utils.stringIsNotEmpty(unionKey)) {//处理组合键，不为空时，选取对应实体
                        String value = getStatusValue(unionKey, statusType);//组合键对应值
                        if (settlementStatus.equals(Integer.parseInt(value))) {
                            basicStatusRelateVo = statusRelateVo;//选取实体
                            break;
                        }
                    }

                }
            }
        }
        return basicStatusRelateVo;
    }

    /**
     * 获取状态业务对象
     *
     * @param state      状态值
     * @param roleType   角色类型
     * @param statusType 状态类型
     * @return
     */
    public static BasicStatusRelateVo getStatusRelate(String state, Integer roleType, String statusType) {
        if (state == null || roleType == null) throw new ServiceException("参数传递有误！");
        if (!Utils.stringIsNotEmpty(statusType)) statusType = StringStaticKey.SETTLEMENT_STATUS;//没值赋,默认为付款方式
        BasicStatusRelateVo basicStatusRelateVo = new BasicStatusRelateVo();
        List<BasicStatusRelateVo> statusRelateVoList = getStatusRelateListByType(statusType);
        if (Utils.collectionIsNotEmpty(statusRelateVoList)) {
            Map map = new TreeMap();
            for (BasicStatusRelateVo statusRelateVo : statusRelateVoList) {//循环判断取值
                String voValue = statusRelateVo.getValue();      //状态值
                Integer voRoteType = statusRelateVo.getRoleType();//角色类型
                String operates = statusRelateVo.getOperates();     //可用操作
                if (roleType.equals(voRoteType) || voRoteType == null) {//得到所有状态
                    map.put(statusRelateVo.getValue(), statusRelateVo.getName());
                }
            }
            basicStatusRelateVo.setStatusMap(map);
        }
        return basicStatusRelateVo;
    }


    /**
     * 根据键名读取redis中的值
     *
     * @param key
     * @return
     */
    public static String getRedisValue(String key) {
        if (!Utils.stringIsNotEmpty(key)) throw new ServiceException("redis读取键异常！");
        return redisBasicService.getValue(key);
    }

}
